﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using MageWars.Support;
using Microsoft.Xna.Framework;


namespace MageWars.GameElements
{
    public abstract class Mage : Movable
    {
        internal const float speed = 1F;
        
        static Mage()
        {
            _mages = new List<Mage>();
        }

        public Mage()
        {
            _mages.Add(this);
        }

        public abstract StateMachine GetSM();

        public static IEnumerable<Mage> Mages
        {
            get
            {
                return _mages.ToArray();
            }
        }

        private static List<Mage> _mages;

        public int HP
        {
            get;
            set;
        }

        internal void Fire()
        {
            if (CastCooldown <= 0)
            {
                CastCooldown = 15;
                var p = new Projectile(this, this.Group);
                p.Position = new Vector3(Position.X, Position.Y, 0);
                p.Direction = this.Direction;

            }
            else
            {
                CastCooldown--;
            }
        }

        internal int CastCooldown = 0;

        public abstract void UpdatePosition();

        internal bool IsWalking = false;
        internal bool Walking1 = false;
        private int WalkingTimer = 10;
        private bool destroyed;

        public bool IsDead
        {
            get
            {
                return destroyed;
            }
        }

        public void VerifyAttackHit()
        {

            var mybb = this.BoundingBox;
            foreach (var s in Projectile.Projectiles)
            {
                if (mybb.Intersects(s.BoundingBox) && s.Caster != this && s.Group != this.Group)
                {
                    HP = HP - 30;
                    s.Destroy();
                    if (HP <= 0)
                    {
                        Destroy();
                    }
                }
            }

        }

        public void Destroy()
        {
            if (!destroyed)
            {
                destroyed = true;
                Position = Vector3.Zero;
                _mages.Remove(this);
            }
        }
        
        //COSAS DEL SALTO

        internal bool IsJumping = false;
        internal const float JumpSpeed = 4f;
        internal const float gravity = 8f;
        internal float JumpTime = 0f;
        internal const float FPS = 45;
        internal float TimePerFrame = 1f / FPS;

        internal void Jump() 
        {
            JumpTime = JumpTime + TimePerFrame;
            float height = (JumpSpeed * JumpTime) - ((gravity * (float)Math.Pow((double)JumpTime, 2))/2);
            if (height > 0)
            {
                this.Position = new Vector3(this.Position.X, this.Position.Y, height);
            }
            else 
            {
                this.Position = new Vector3(this.Position.X, this.Position.Y, 0);
                IsJumping = false;
                JumpTime = 0;
            }
        }

        //COLISIONES
        //Por ahora solo chequea colisiones con magos
        internal bool CheckCollisions()
        {
            foreach (var m in Mage.Mages) 
            {
                if (m != this && this.BoundingBox.Intersects(m.BoundingBox)) 
                {
                    return true;
                }
            }
            return false;
        }

        //Evasion de colisiones
        public float radio = 10f;

        public override Texture2D CurrentTexture()
        {
            if (!IsWalking)
            {
                if (this.Group == Team.TeamBlue)
                {
                    return Textures.Team1Standing;
                }
                else
                {
                    return Textures.Team2Standing;
                }
            }
            else
            {
                WalkingTimer--;
                if (WalkingTimer <= 0)
                {
                    Walking1 = !Walking1;
                    WalkingTimer = 10;
                }
                if (Walking1)
                {
                    if (this.Group == Team.TeamBlue)
                    {
                        return Textures.Team1Walking1;
                    }
                    else
                    {
                        return Textures.Team2Walking1;
                    }


                }
                else
                {

                    if (this.Group == Team.TeamBlue)
                    {
                        return Textures.Team1Walking2;
                    }
                    else
                    {
                        return Textures.Team2Walking2;
                    }
                }

            }

        }

    }



    public enum Behavior
    {
        Wander,
        Seek
    }
}